package com.cnswhy.cloud.server.merchant.controller.customer;

import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.common.entity.mq.UpdateCustomerSplitMQ;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerSplit;
import com.cnswhy.cloud.server.core.dto.CustomerSplitSaveDTO;
import com.cnswhy.cloud.server.core.dto.CustomerSplitUpdateDTO;
import com.cnswhy.cloud.server.core.dto.CustomerSplitPageDTO;
import com.cnswhy.cloud.server.core.entity.CustomerWallet;
import com.cnswhy.cloud.server.core.entity.WalletTransDtl;
import com.cnswhy.cloud.server.core.service.CustomerSplitService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-12-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customerSplit")
@Api(value = "CustomerSplit", tags = "")
//@PreAuth(replace = "customerSplit:")
public class CustomerSplitController extends SuperController<CustomerSplitService, Long, CustomerSplit, CustomerSplitPageDTO, CustomerSplitSaveDTO, CustomerSplitUpdateDTO> {
    @Autowired
    CustomerService customerService;
    @Autowired
    MessageService messageService;

    @Override
    public R<CustomerSplit> handlerUpdate(CustomerSplitUpdateDTO model) {
        if(model.getSplitRate().compareTo(0L) < 0 || model.getSplitRate().compareTo(100L) > 0){
            return R.fail("配比在0-100之间");
        }
        model.setCurrRate(BigDecimal.valueOf(model.getSplitRate()));
        return R.successDef();
    }

    @Override
    public R<CustomerSplit> handlerAfterUpdate(CustomerSplitUpdateDTO customerSplitUpdateDTO, CustomerSplit customerSplit) {
        UpdateCustomerSplitMQ updateCustomerSplitMQ = UpdateCustomerSplitMQ.builder().customerId(customerSplit.getDownCustomerId()).splitType(customerSplit.getSplitType()).build();
        messageService.sendQueueByObject(AmqpExchange.UPDATE_CUSTOMER_SPLIT_MESSAGE,updateCustomerSplitMQ);
        return R.successDef();
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomerSplit> customerSplitList = list.stream().map((map) -> {
            CustomerSplit customerSplit = CustomerSplit.builder().build();
            return customerSplit;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customerSplitList));
    }
}
